<?php
namespace app\admin\controller;

use think\Controller;
use app\admin\model\Login as LoginModel;

class Login extends Controller
{
    /**
     * 用户登录界面
     * @return mixed
     */
    public function index(){
        //若已登录
        if (is_login()){
            $this->redirect('Index/index');
        }
        return $this->fetch();
    }

    /**
     * 提交登录
     */
    public function login(){
        if (!$this->request->isPost()){
            $this->error('非法提交参数！');
        }
        //验证 //todo:使用框架的内置验证功能
        $param = $this->request->param();
        if (!captcha_check($param['verification_code'])){
            $this->error('验证码错误！');
        }
        $username = trim($param['username']);
        $password = trim($param['password']);
        if (!$username || !$password){
            $this->error('账号和密码不能为空！');
        }
        //开始登录
        $login = new LoginModel();
        $result = $login->login($username, $password);
        if ($result['status_code'] != 0){
            $this->error($result['status_info']);
        }
        $this->success('登录成功。', 'Index/index');
    }

    /**
     * 注销
     */
    public function logout(){
        $login = new LoginModel();
        $login->logout();
        $this->success('注销成功。', 'Login/index');
    }

}